/* 
 * File:   TSPOrganism.cpp
 * Author: wasp
 * 
 * Created on December 15, 2014, 12:37 AM
 */

#include <iostream>

#include "TSPOrganism.h"
#include "GA/Organism.h"
//#include "GA/Genome.h"
#include "GA/Gene.h"
#include "GA/IntGene.h"
#include "TSPGenome.h"
#include "Cities.h"
namespace tsp {



TSPOrganism::TSPOrganism() : practical::ga::Organism() {
	std::vector<Cities::City> cities = Cities::instance().getCities();
	this->initialize(cities.size() * 11);
}

TSPOrganism::TSPOrganism(unsigned int initial_size) : practical::ga::Organism(initial_size){
	this->initialize(initial_size);
}

TSPOrganism::TSPOrganism(const practical::ga::Organism& other) : practical::ga::Organism(other){
	// alle kopiervorgaenge werden von Organism bereits erledigt
}

/**
 * helfer methode die im constructor aufgerufen wird
 * @param initial_size
 */
void TSPOrganism::initialize(unsigned int initial_size) {
	std::vector<Cities::City> cities = Cities::instance().getCities();

	int l = cities.size();
	std::vector<int> ids;
	// initialize ids for later random generation
	for (int i=0; i<l; i++) {
		ids.push_back(i);
	}
	
	for (int x=0; x<initial_size; x++) {
		std::vector<practical::ga::IntGene> gs;
		for (int i=ids.size(); i > 0;) {
			int currentIndex = i-1;
			// this gives us random ids and ensure not to give any id twice
			int destIndex = practical::ga::util::randInt(i);
			int selected = ids[destIndex];
			ids[destIndex] = ids[currentIndex];
			ids[currentIndex] = selected;

			// select random id
			practical::ga::IntGene g(selected);
			gs.push_back(g);

			i = currentIndex;
		}
		TSPGenome* ge = new TSPGenome(gs); // delete findet in der clearGenomes statt
		genomes.push_back(ge);
	}
}

}

